<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\cache\driver\Redis;
class Saledocker extends Auth
{

	public function __construct(Request $request){
        parent::__construct($request);
        $this->data = $request->param();
        $this->lang = isset($this->data['lang']) && $this->data['lang'] ? $this->data['lang'] : is_lang();
        $this->user_id = session('userid') == null ? 1 : session('userid');
        $this->saleModel = model('SaleModel');
        $this->AdminWarehouse_id = $this->saleModel->getAdminWarehouse($this->user_id);
    }

    public function list(Request $request){
        if(isset($this->data['request']) && $this->data['request'] == 'requestAjax'){
            // dump($this->data);die;
            $page = "1,1000000";
            $return['pageSum'] = 0;
            if(isset($this->data['page']) && isset($this->data['pageSize'])){
                $page = $this->data['page'].','.$this->data['pageSize'];
                $return['page'] = $this->data['page'];
            } else{
                $return['page'] = 1;
            }
            
            if( isset($this->data['requestType']) && ($this->data['requestType'] == 'pickuping' || $this->data['requestType'] == 'pauseing' || $this->data['requestType'] == 'finish' || $this->data['requestType'] == 'cancel')) {
                $returnData = $this->getListData($page);
                $return['list'] = $returnData['list'];
                $return['pageSum'] = $returnData['pageSum'];
            }

            if( isset($this->data['requestType']) && $this->data['requestType'] == 'list' ){
                $return['pageSum'] = hasData('make_pickup_detail',['pickup_id'=>0,'warehouse_id'=>$this->AdminWarehouse_id]);
                $list = getDatas('make_pickup_detail',['*'],['pickup_id'=>0,'warehouse_id'=>$this->AdminWarehouse_id],'',$page);
                if(!empty($list)){
                    $return['list'] = $this->getList($list);
                }
            } 

            // 是否有未完成的任务
            $return['hasTask'] = 0 ;
            // dump($return);die;
            return json_encode($return);
        }else{
            return view('list',['isshow'=>1]);
        }
    }

    function getList($data)
    {   
        $return = [];
        $sale_ids = '';
        $i=0;
        $key = [];
        foreach ($data as $k => $v) {
            $res = $v;
            $res['create_name'] = getValue('admin',['id'=>$v['create_id']],'nickname');
            $res['create_time'] = getTimeDates($v['create_at'],$this->lang);
            $res['targetName'] = $this->getTargetName($v['target']);
            $res['warehouseArea_name'] = $this->saleModel->getWarehouseArea(false,['id'=>$v['area_id']],'name');
            $res['proInfo'] = getProAndProchar($v,$this->lang,48);
            $res['storeInfo'] = $this->getstore2place($v['pro_id'],$v['prochar_id'],$v['warehouse_id'],$res['proInfo']['prochar_qty'],$this->lang);    
            if($v['sale_id'] == 0 || !strstr($sale_ids,(String)$v['sale_id'])){
                $return[$i][] = $res; 

                if($v['sale_id'] > 0){
                    $key[$v['sale_id']] = $i;
                } 
                $i++;
                $sale_ids .= $v['sale_id'].',';
            }else if($v['sale_id'] > 0 && strstr($sale_ids,(String)$v['sale_id'])){
                $return[$key[$v['sale_id']]][] = $res; 
            }
        }
        // dump($return);die;
        return $return;
    }

    function getNavPageNum()
    {
        $return['listNum'] = hasData('make_pickup_detail',['pickup_id'=>0,'warehouse_id'=>$this->AdminWarehouse_id]);
        $return['pickupingNum'] = $this->setThisRequestType('pickuping');
        $return['pauseingNum'] =  $this->setThisRequestType('pauseing');
        $return['finishNum'] = $this->setThisRequestType('finish');
        $return['cancelNum'] = $this->setThisRequestType('cancel');
        return json_encode($return);

    }

    function setThisRequestType($requestType)
    {
        $this->data['requestType'] = $requestType;
        $this->data['is_me'] = false;
        return $this->getListData(1,1000000)['pageSum'];
    }

    function getListData($page)
    {   
        $where['warehouse_id'] = $this->AdminWarehouse_id;

        if($this->data['is_me'] == 'true') $where['create_id'] = $this->user_id;

        if($this->data['requestType'] == 'finish'){
            $where['finish_at'] = ['gt',0];
        }else if($this->data['requestType'] == 'cancel'){
            $where['cancel_at'] = ['gt',0];
        }else{
            $where['finish_at'] =0;
            $where['pause_at'] =0;
            $where['start_at'] = ['gt',0];
        }
        
        if($this->data['requestType'] == 'pauseing') $where['pause_at'] = ['gt',0];
        

        $return['pageSum'] = 0;
        $list = getDatas('make_pickup',['*'],$where,'id desc',$page);
        if(!empty($list)){
            foreach ($list as $k => $v) {
                $list[$k]['detail'] = getDatas('make_pickup_detail',['*'],['pickup_id'=>$v['id'],'getback_at'=>0]);

                $list[$k]['qty_sum'] = 0;
                if(!empty($list[$k]['detail'])){
                    $return['pageSum'] += count($list[$k]['detail']);
                    foreach ($list[$k]['detail'] as $key => $value) {
                        $list[$k]['create_name'] = getValue('admin',['id'=>$value['create_id']],'nickname');
                        $list[$k]['create_time'] = getTimeDates($value['create_at'],$this->lang);
                        $list[$k]['qty_sum'] += $value['qty'];
                        $list[$k]['detail'][$key]['warehouseArea_name'] = $this->saleModel->getWarehouseArea(false,['id'=>$value['area_id']],'name');
                        $list[$k]['detail'][$key]['targetName'] = $this->getTargetName($value['target']);
                        $list[$k]['detail'][$key]['proInfo'] = getProAndProchar($value,$this->lang,48);

                        $list[$k]['detail'][$key]['storeInfo'] = $this->getstore2place($value['pro_id'],$value['prochar_id'],$value['warehouse_id'],$list[$k]['detail'][$key]['proInfo']['prochar_qty'],$this->lang); 
                        if($this->data['requestType'] == 'pauseing' || $this->data['requestType'] == 'cancel' ){
                            $list[$k]['pause_name'] = getValue('admin',['id'=>$v['pause_id']],'nickname');
                            $list[$k]['pause_time'] = getTimeDates($v['pause_at'],$this->lang);

                            $where_ = ['sale_id'=>$value['sale_id'],'pro_id'=>$value['pro_id'],'prochar_id'=>$value['prochar_id']];
                            // 仓管员
                            $list[$k]['detail'][$key]['docker_name'] = $this->saleModel->getDockerAOrPacker('make_pickup_detail',$where_,'pickup_id','make_pickup');
                            // 包装主管（加工）  
                            $list[$k]['detail'][$key]['packer_name'] = $this->saleModel->getDockerAOrPacker('make_process_detail',$where_,'process_id','make_process');
                    
                        }
                        if($this->data['requestType'] == 'cancel'){
                            $list[$k]['detail'][$key]['placeSelected'] = -1;
                            if($value['sale_id'] > 0){
                                $wh = ['zu.pickup_id'=>$v['id'],'zu.make_id'=>$value['make_id'],'zu.pro_id'=>$value['pro_id'],'zu.prochar_id'=>$value['prochar_id'],'zu.warehouse_id'=>$value['warehouse_id']];
                                // dump($wh);die;
                                $res = getSearch('make_pickup_store','zu',['zu.*,p.name placeName'],$wh,[['warehouse_place p','zu.place_id = p.id','LEFT']],'zu.id desc');
                                $list[$k]['detail'][$key]['placeInfo'] =  arraySort($res, 'qty', SORT_DESC);
                                $list[$k]['detail'][$key]['placeSelected'] =  $list[$k]['detail'][$key]['placeInfo'][0]['place_id'] ;

                            }else{
                                $res1['placeName'] = '没place位置信息';
                                $res1['qty'] = $value['qty'];
                                $list[$k]['detail'][$key]['placeInfo'][] = $res1;
                            }
                        }
                        if($this->data['requestType'] == 'finish'){
                            $list[$k]['time_difference'] = time() - $value['create_at'];
                        }

                        if($this->data['requestType'] == 'pickuping'){
                            $list[$k]['destination'] = $this->getDestination($value['create_id'],$value['prochar_id']);
                        }
                    }
                }
            }
        }
        $return['list'] = $list;
        return $return;
    }

    function getDestination($user_id,$prochar_id)
    {
        $position_id = getValue('admin',['id'=>$user_id],'position_id');
        $where['warehouse_id'] = $this->AdminWarehouse_id;
        if($position_id == 26){
            $where['type_id'] = ['in','1,2'];
        }else if($position_id == 27){
            $where['type_id'] = 3;
        }else{
            $where['type_id'] = ['in','1,2,3'];
        }
        $return = getDatas('warehouse_area',['id,type_id,name'],$where);

        // 重新排序
        $returnData = [];
        if(!empty($return)){
            $where_['warehouse_id'] = $this->AdminWarehouse_id;
            foreach ($return as $k => $v) {
                $where_['area_id'] = $v['id'];
                $where_['prochar_id'] = $prochar_id;
                if(hasData('store2area',$where_)){
                    array_unshift($returnData, $v);
                } else{
                    $returnData[] = $v;
                }
            }    
        }
        
        return $returnData;
    }

    // 新订单 开始提货-保存
    function savePickupDetail()
    {
        // dump($this->data);die;
        if(isset($this->data['list']) && !empty($this->data['list'])){

            $pickup_id = insertData('make_pickup',['warehouse_id'=>$this->AdminWarehouse_id,'start_at'=>time(),'create_id'=>$this->user_id,'create_at'=>time()]);
            foreach ($this->data['list'] as $k => $v) {
                $res = updateData('make_pickup_detail',['id'=>$v['id']],['pickup_id'=>$pickup_id,'start_at'=>time()]);
                $this->saleModel->addSaleLog($v['sale_id'],'makePickup','仓管员开始提货');
            }

            return json_encode(array('status'=>1,'msg'=>'提货成功'));
        }
    }

    // 提货中 提货完成-保存
    function savePickupingFulfil()
    {   
        // 未完成问题：make_pickup_store新增数据时place_id绑定
        //  当$this->data['right']['detail']['sale_id'] != 0 的时候 添加make_pickup_store 数据
        //  更新库存qty
        if(isset($this->data['storeInfo'])){
                $sale_id = 0;
                $saleData = [];
                $saleData_ = [];
                foreach ($this->data['right']['detail'] as $k1 => $v1) {
                    $v1['qty_pcs'] = $num = $v1['qty']/$v1['proInfo']['prochar_qty'];   // 转换一下数量单位
                    if($v1['sale_id'] > 0){
                        $saleData[] = $v1; 
                    }else{
                        $saleData_[] = $v1; 
                    }
                    // 编辑store2area qty
                    if($v1['area_id']){
                        $store2areaWhere = ['warehouse_id'=>$v1['warehouse_id'],'pro_id'=>$v1['pro_id'],'prochar_id'=>$v1['prochar_id'],'area_id'=>$v1['area_id']];
                        $store2area = Db::name('store2area')->where($store2areaWhere)->find();
                        $volume = $v1['proInfo']['ctn_m3']*$v1['qty_pcs'];
                        if($store2area){
                            updateData('store2area',$store2areaWhere,['qty'=>$store2area['qty']+$v1['qty'],'volume'=>$volume+$store2area['volume']]);
                        }else{
                            insertData('store2area',['qty'=>$v1['qty'],'volume'=>$volume,'warehouse_id'=>$v1['warehouse_id'],'pro_id'=>$v1['pro_id'],'prochar_id'=>$v1['prochar_id'],'area_id'=>$v1['area_id']]);
                        }
                    }
                    // 改变成完成状态
                    updateData('make_pickup_detail',['id'=>$v1['id']],['finish_at'=>time(),'area_id'=>$v1['area_id']]);
                    // 判断当前detail所在的 sale_make 旗下的 pickup_detail是否全部已完成，全部已完成 则改变sale_make 的pickup_at > 0
                    if(!hasData('make_pickup_detail',['make_id'=>$v1['make_id'],'finish_at'=>0])){
                        updateData('sale_make',['id'=>$v1['make_id']],['pickup_at'=>time()]);
                        $this->saleModel->addSaleLog($v1['sale_id'],'makePickup','仓管员提货完成');
                    }
                    
                }
                if(isset($this->data['storeInfo']['detail'])){
                    foreach ($this->data['storeInfo']['detail'] as $k => $v) {
                        if($v['inputValue'] > 0){
                            // 减总库存qty
                            if($v['warehouse_id'] != 0){
                                Db::name('store')->where(['pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id'],'warehouse_id'=>0])->setDec('qty',($v['inputValue']*$this->data['proInfo']['prochar_qty']));
                            }
                            // 本地仓库总库存
                            if($v['place_id'] != 0){
                                Db::name('store')->where(['pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id'],'warehouse_id'=>$v['warehouse_id'],'place_id'=>0])->setDec('qty',($v['inputValue']*$this->data['proInfo']['prochar_qty']));
                            }
                            // 被选中的仓库、仓库位置减去需要的数量
                            Db::name('store')->where(['pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id'],'warehouse_id'=>$v['warehouse_id'],'place_id'=>$v['place_id']])->setDec('qty',($v['inputValue']*$this->data['proInfo']['prochar_qty']));
                        }
                    } 
                    // 获取从新排序后的store数据
                    $storeSortData = arraySort($this->data['storeInfo']['detail'], 'inputValue', SORT_ASC);
                    // dump($saleData);die;
                    // storeSortData 符合right.detal.sale_id = 0,的数据剔除掉
                    //散货的(sale_id = 0)，不生成 make_pickup_store 这里的话，就不考虑了，但是这样的话，这里还是要减去store对应数据的qty。store2area 对应的数据增加qty
                    if(!empty($saleData_)){
                        foreach ($saleData_ as $k2 => $v2) {
                            $num = $v2['qty_pcs'];
                            if($num > 0){
                                foreach ($storeSortData as $k3 => $v3) {
                                    // 如果make_pickup_detail.qty大于store.input.qty 则直接在数组中删除该元素
                                    if($num > 0){
                                        if( $num >  $v3['inputValue'] ) {
                                            unset($storeSortData[$k3]);
                                            $num = $num - $v3['inputValue'];
                                        }else{
                                            $storeSortData[$k3]['inputValue'] = $v3['inputValue'] -  $num ;
                                            $num = 0;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // dump($storeSortData);

                    //  按顺序优先清掉数量少的，不然，产品到处放，容易丢也不好找
                    //  一：如果两者相等，直接新增 make_pickup_store 数据
                    if(!empty($saleData)){
                        foreach ($saleData as $k4 => $v4) {
                            foreach ($storeSortData as $k5 => $v5) {
                                if($v4['qty_pcs'] == $v5['inputValue']){  
                                    $insertData['pickup_id'] = $this->data['right']['id'];
                                    $insertData['make_id'] = $v4['make_id'];
                                    $insertData['qty'] = $v4['qty'];
                                    $insertData['pro_id'] = $v4['pro_id'];
                                    $insertData['prochar_id'] = $v4['prochar_id'];
                                    $insertData['warehouse_id'] = $v4['warehouse_id'];
                                    $insertData['place_id'] = $v5['place_id'];
                                    $insertData['create_id'] = $this->user_id;
                                    $insertData['create_at'] = time();
                                    $res1 = insertData('make_pickup_store',$insertData);
                                    unset($saleData[$k4]);
                                    unset($storeSortData[$k5]);
                                }
                            }
                        }
                    }
                    //  二：
                    $saleData = arraySort($saleData, 'qty', SORT_ASC);
                    if(!empty($saleData)){
                        foreach ($saleData as $k6 => $v6) {
                            $num6 = $v6['qty_pcs'];
                            foreach ($storeSortData as $k7 => $v7) {
                                if($num6 > 0 ){
                                    $insertData['pickup_id'] = $this->data['right']['id'];
                                    $insertData['make_id'] = $v6['make_id'];
                                    $insertData['qty'] = $v6['qty'];
                                    $insertData['pro_id'] = $v6['pro_id'];
                                    $insertData['prochar_id'] = $v6['prochar_id'];
                                    $insertData['warehouse_id'] = $v6['warehouse_id'];
                                    $insertData['place_id'] = $v7['place_id'];
                                    $insertData['create_id'] = $this->user_id;
                                    $insertData['create_at'] = time();
                                    $res1 = insertData('make_pickup_store',$insertData);
                                    if($num6 > $v7['inputValue']){ // 如果两者相等，直接新增 make_pickup_store 数据
                                        unset($saleData[$k6]);
                                        $num6 -= $v7['inputValue'];
                                    }else{
                                        $num6 = 0;
                                        $storeSortData[$k7]['inputValue'] = $v7['inputValue'] - $num6;
                                    }  
                                }
                            }
                        }
                    }        
                }
                
                //  改变成完成状态
                updateData('make_pickup',['id'=>$this->data['right']['id']],['finish_at'=>time()]);

            return json_encode(array('status'=>1,'msg'=>'提货成功'));
        }
    }

    function pickupMissing()
    {
        if($this->data['requestType'] == 'get'){
            $whereCreateAt = getValue('sale',['id'=>$this->data['sale_id']],'create_at');
            $list = Db::name('make_pickup_detail')
                        ->where('sale_id','in',function($query) use ($whereCreateAt){
                            $query->name('sale')->where(['create_at'=>['gt',$whereCreateAt-1]])->field('id');
                        })
                        ->where(['warehouse_id'=>$this->AdminWarehouse_id,'pro_id'=>$this->data['pro_id'],'prochar_id'=>$this->data['prochar_id']])
                        ->select();
            $qtySum = 0;
            if(!empty($list)){
                foreach ($list as $k => $v) {
                    $list[$k]['target_name'] = $this->getTargetName($v['target']);
                    $list[$k]['area_name'] = $this->saleModel->getArea_name($v['target'],$v['area_id']);
                    $qtySum += $v['qty'];
                }
            }
            // dump($list);die;
            $returnData['list'] = $list;
            $returnData['qtySum'] = $qtySum;
            return json_encode($returnData);    
        }else if($this->data['requestType'] == 'save'){
            // dump($this->data);die;
            $storeMissing['pro_id']       = $this->data['pro_id'];
            $storeMissing['prochar_id']   = $this->data['prochar_id'];
            $storeMissing['warehouse_id'] = $this->data['warehouse_id'];
            $storeMissing['missing_qty']  = $this->data['pickupMissingQty'];
            $storeMissing['store_qty']    = $this->data['storeInfo']['store']['qty'];
            $storeMissing['create_id']    = $this->user_id;
            $storeMissing['create_at']    = time();
            $storeMissing['update_id']    = $this->user_id;
            $storeMissing['update_at']    = time();
            $storeMissing_id = insertData('store_missing',$storeMissing);
            if($storeMissing_id){
                if(isset($this->data['saleList'])){
                    $storeMissingDetail['missing_id'] = $storeMissing_id;
                    foreach ($this->data['saleList'] as $k => $v) {
                        $storeMissingDetail['sale_make_id'] = $v['make_id'];
                        $storeMissingDetail['sale_id'] = $v['sale_id'];
                        insertData('store_missing_detail',$storeMissingDetail);
                        // 受影响的订单暂停
                        $pause['pause_id'] = $this->user_id;
                        $pause['pause_at'] = time();
                        updateData('sale_make',['id'=>$v['make_id']],$pause);
                        updateData('make_pickup',['id'=>$v['pickup_id']],$pause);
                        updateData('make_pickup_detail',['id'=>$v['id']],$pause);
                        $this->saleModel->addSaleLog($v['sale_id'],'makePickup','仓管员登记缺货');
                    }
                }
                return json_encode(array('status'=>1,'msg'=>'缺货登记成功'));    
            }else{
                return json_encode(array('status'=>2,'msg'=>'缺货登记失败'));
            }
        }
    }

    //  取消完成
    function finishCancel()
    {   
        if(isset($this->data['id'])){
            $res = updateData('make_pickup',['id'=>$this->data['id']],['finish_at'=>0]);

            $pickup_detail = getDatas('make_pickup_detail',['*'],['pickup_id'=>$this->data['id'],'finish_at'=>['gt',0]]);
            $sale_id = 0;
            foreach ($pickup_detail as $k => $v) {
                $sale_id = $v['sale_id'];
                $where['pro_id'] = $v['pro_id'];
                $where['prochar_id'] = $v['prochar_id'];
                $where['pickup_id'] = $this->data['id'];
                $where['warehouse_id'] = $v['warehouse_id'];
                $make_pickup_store = getDatas('make_pickup_store',['*'],$where);

                $qty = $v['qty'];
                if($qty > 0){
                    foreach ($make_pickup_store as $k1 => $v1) {
                        $where_['pro_id'] = $v1['pro_id'];
                        $where_['place_id'] = $v1['place_id'];
                        $where_['prochar_id'] = $v1['prochar_id'];
                        $where_['warehouse_id'] = $v1['warehouse_id'];

                        if($qty == $v1['qty']){
                            $qty_ = $qty;
                            $qty = 0;
                            db::name('make_pickup_store')->where(['id'=>$v1['id']])->delete();

                        } else if($qty > $v1['qty']){
                            $qty -= $v1['qty'];
                            $qty_ = $qty;
                            db::name('make_pickup_store')->where(['id'=>$v1['id']])->delete();

                        } else if($qty < $v1['qty']){
                            $qty_ = $qty;
                            $qty = 0;
                            db::name('make_pickup_store')->where(['id'=>$v1['id']])->setDec('qty',$qty_);
                        } 

                        if($qty_ > 0){
                            Db::name('store')->where($where_)->setInc('qty',$qty_);

                            // 总库存 添加回来数据
                            if($v1['warehouse_id'] != 0){ 
                                $where_['place_id'] = 0;
                                $where_['warehouse_id'] = 0;
                                Db::name('store')->where($where_)->setInc('qty',$qty_);
                            } 
                            // 本地总库存 添加回来数据
                            if($v1['place_id'] != 0){ 
                                $where_['place_id'] = 0;
                                $where_['warehouse_id'] = $v1['warehouse_id'];
                                Db::name('store')->where($where_)->setInc('qty',$qty_);
                            } 
                        }
                    }
                }
                
            }

            $this->saleModel->addSaleLog($sale_id,'makePickup','仓管员取消提货');
            return json_encode(array('status'=>1,'msg'=>'取消成功'));
        }
    }

    // 取回
    function getBack()
    {
        if(isset($this->data['id']) && $this->data['requestType'] == 'getPlace'){
            $warehouse_id = getValue('make_pickup',['id'=>$this->data['id']],'warehouse_id');
            return json_encode( getDatas('warehouse_place',['id,name'],['warehouse_id'=>$warehouse_id]));

        }else if(isset($this->data['id']) && $this->data['requestType'] == 'save'){
            if(!empty($this->data['detail'])){
                $this->saveCancel($this->data['detail']);
            }
        }
    }

    /**
    * 取回保存
    * 每个取回摆放位置 ： placeSelected
    * 每个取回的数量：getBackQty
    * 需要判断原本的数量 qty 和取回数量 对比，取回数量 getBackQty 只能小于等于 qty,
    * 当 getBackQty < qty时 需要把多余的取回到散货区。
    * 当 sale_id == 0 的时候，直接取回到散货区

        取回整理：
            一：取回数据 make_pickup_detail.sale_id == 0时，不需要管取回数量和选中的取回位置，直接取回到store2area.place == 2 ,同一warehouse_id/pro_id/prochar_id,如果存在则累加，不存在则新增。
            二：取回数据 make_pickup_detail.sale_id > 0时，根据选中的place_id,直接取回到store.place_id == 选中的place_id,同一warehouse_id/pro_id/prochar_id,如果存在则累加，不存在则新增（不管make_pickup_detail）。
            三：取回的数数量 getbackqty < make_pickup_detail.qty，则多出来的数量(make_pickup_detail.qty - getBackQty )直接取回到store2area.place == 2 ,同一warehouse_id/pro_id/prochar_id,如果存在则累加，不存在则新增。
    */
    function saveCancel($data)
    {
        foreach ($this->data['detail'] as $k => $v) {
            $where = [];
            // sale_id == 0 直接存入散货区
            if($v['sale_id'] <= 0 ){
                $this->savelCancelToStore2area($v);
            //  如果sale_id 存在，根据选择的取回摆放位置，placeSelected == store.place_id，然后把取回的数量 直接累加到store.place 数据中。
            }else{
                $where = [ 'warehouse_id'=>$v['warehouse_id'],'place_id'=>$v['placeSelected'],'pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id'] ];
                $store = getDatas('store',['id,qty'],$where);
                if($store){
                    $res = updateData('store',['id'=>$store[0]['id']],['qty'=>$store['0']['qty']+$v['getBackQty'] ]);
                }else{
                    $where['qty'] =  $v['getBackQty'];
                    $res = insertData('store',$where);
                }
                // 如果原本数量，大于取回数据，则多出来的数量存入散货区
                if( $v['qty'] - $v['getBackQty'] > 0 ){
                    $v['getBackQty'] = $v['qty'] - $v['getBackQty'];
                    $this->savelCancelToStore2area($v);
                }
            }

            // 修改make_pickup_detail.getback_at
            $res = updateData('make_pickup_detail',['id'=>$v['id']],['getback_id'=>$this->user_id,'getback_at'=>time()]);
        }
        return json_decode(array('status'=>1,'msg'=>'取回成功'));
    }

    // 取回数量 放入散货区
    function savelCancelToStore2area($data)
    {
        $where = ['warehouse_id'=>$data['warehouse_id'],'area_id'=>2,'pro_id'=>$data['pro_id'],'prochar_id'=>$data['prochar_id'] ];
        $store2area = getDatas('store2area',['id,qty,volume'],$where);
        if($store2area){
            $updateData = ['qty'=>$data['getBackQty']+$store2area[0]['id'],'volume'=>$data['getBackQty']*$data['proInfo']['ctn_m3']+$store2area[0]['volume'] ];
            $res = updateData('store2area',[ 'id'=>$store2area[0]['id'] ],$updateData);
        }else{
            $where['qty'] = $data['getBackQty'];
            $where['volume'] = $data['getBackQty']*$data['proInfo']['ctn_m3'];
            $res = insertData('store2area',$where);
        }
    }


    function getstore2place($pro_id,$prochar_id,$warehouse_id,$prochar_qty)
    {
        $return['store'] = [];
        $return['store']['id'] = $warehouse_id;
        $return['store']['name'] = '本地库存';
        $return['store']['qty'] = getValue('store',['warehouse_id'=>$warehouse_id,'pro_id'=>$pro_id,'prochar_id'=>$prochar_id],'qty');

        $return['detail'] = getSearch('store','zu',['zu.*,p.name'],['zu.warehouse_id'=>$warehouse_id,'zu.pro_id'=>$pro_id,'zu.prochar_id'=>$prochar_id,'zu.place_id'=>['gt',0]],[['warehouse_place p','zu.place_id = p.id']],'zu.id desc');
        return $return;
    }


    function getstore2area($pro_id,$prochar_id,$warehouse_id)
    {
        $return = getDatas('store',['*'],['warehouse_id'=>$warehouse_id,'pro_id'=>$pro_id,'prochar_id'=>$prochar_id]);

        $return[0]['warehouse_area'] = getSearch('store2area','zu',['zu.*,a.name'],['zu.prochar_id'=>$prochar_id,'zu.pro_id'=>$pro_id,'zu.warehouse_id'=>$warehouse_id],[['warehouse_area a','zu.area_id = a.id','LEFT']],'zu.id desc');
        return $return[0];
    }

    function getTargetName($type)
    {
        switch ($type) {
            case 0:
                return '销售订单';
                break;
            case 1:
                return '散货';
                break;
            case 2:
                return '包装部';
                break;
        }
    }

}
